<template>
  <div>
    <!-- <h3>form组件</h3> -->
    <el-dialog
      :title="status.isAdd ? '商品分类添加' : '商品分类编辑'"
      :visible.sync="status.isshow"
      @closed="closed"
    >
      <el-form :model="form">
        {{ form }}
        <el-form-item label="上级分类" label-width="120px">
          <el-select v-model="form.pid" placeholder="请选择上级分类">
            <el-option label="顶级分类" :value="0"></el-option>
            <el-option
              v-for="item in list"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="分类名称" label-width="120px">
          <el-input v-model="form.catename" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="图片" label-width="120px">
          <div class="file">
            <img :src="imgUrl" v-if="imgUrl" alt="" />
            <span>+</span>
            <input type="file" @change="changeFile" v-if="status.isshow" />
          </div>
        </el-form-item>

        <el-form-item label="状态" label-width="120px">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="isshow = status.isshow = false">取 消</el-button>
        <el-button type="primary" v-if="status.isAdd" @click="add"
          >添 加</el-button
        >
        <el-button type="primary" @click="update" v-else>修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  reqcateadd,
  reqcateedit,
  reqcateinfo,
} from "../../../http/api";
import { successAlert } from "../../../utils/alert";
import { mapGetters, mapActions } from "vuex";
import path from "path";
export default {
  props: ["status"],
  data() {
    return {
      // 添加接口初始化数据
      form: {
        pid: "",
        catename: "",
        img: "",
        status: 1,
      },
      // 图片地址初始值
      imgUrl: "",
    };
  },
  methods: {
    ...mapActions({
      reqList: "cate/reqList",
    }),
    empty() {
      this.form = {
        pid: "",
        catename: "",
        img: "",
        status: 1,
      };
      this.imgUrl = "";
    },
    add() {
      reqcateadd(this.form).then((res) => {
        if (res.data.code == 200) {
          // 弹成功
          successAlert(res.data.msg);
          // 清空
          this.empty();
          //  弹框消失
          this.status.isshow = false;
          // 列表重新渲染
          this.reqList();
        }
      });
    },

    // 点击编辑之后
    getOne(id) {
      reqcateinfo({
        id: id,
      }).then((res) => {
        if (res.data.code == 200) {
          this.form = res.data.list;
          this.imgUrl = this.$pre + res.data.list.img;
          // 补一个id删除时要用
          this.form.id = id;
        }
      });
    },
    // 点击修改
    update() {
      reqcateedit(this.form).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          this.empty();
          this.status.isshow = false;
          this.reqList();
        }
      });
    },
    // 关闭弹框事件
    closed() {
      if (!this.status.isAdd) {
        this.empty();
      }
    },
    // 上传图片事件
    changeFile(e) {
      console.log(e);
      // 取出文件对象
      let file = e.target.files[0];
      //判断大小 1M=1024KB 1KB=1024B   file.size B
      if (file.size > 2 * 1024 * 1024) {
        errorAlert("文件大小最大2M");
        return;
      }
      // 判断文件类型 file name: "2.2.2.2.2.jpeg"
      let imgArr = [".jpg", ".jpeg", ".gif", ".png"];
      let extname = path.extname(file.name);
      if (!imgArr.includes(extname)) {
        errorAlert("请上传正确的图片格式");
        return;
      }
      // 根据文件生成一个图片地址，赋值给imgUrl
      this.imgUrl = URL.createObjectURL(file);
      //将文件存到form.img
      this.form.img = file;
    },
  },
  mounted() {},
  computed: {
    ...mapGetters({
      list: "cate/list",
    }),
  },
};
</script>

<style scoped lang="less">
@import "../../../less/index.less";
.file {
  width: 150px;
  height: 150px;
  border-radius: 5px;
  border: 1px dashed @primary;
  position: relative;
}
.file input {
  width: 100%;
  height: 100%;
  position: absolute;
  left: 0;
  top: 0;
  opacity: 0;
}
.file span {
  display: inline-block;
  width: 100%;
  height: 100%;
  text-align: center;
  line-height: 150px;
  font-size: 30px;
}
.file img {
  width: 100%;
  height: 100%;
  position: absolute;
  left: 0;
  top: 0;
}
</style>